package com.example.recite_words.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import com.example.recite_words.Bean.User;
import com.example.recite_words.MainActivity;
import com.example.recite_words.R;
import com.example.recite_words.util.Config;
import com.example.recite_words.util.Send_data;


import java.net.URLEncoder;

public class Register extends MainActivity {
    JSONObject jsonObject = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Button back_login=findViewById(R.id.back_login);
        EditText nickname = findViewById(R.id.re_username);
        nickname.setHorizontallyScrolling(true);
        EditText email = findViewById(R.id.re_email);
        email.setHorizontallyScrolling(true);
        email.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        EditText password = findViewById(R.id.re_password);
        password.setHorizontallyScrolling(true);
        EditText affirm = findViewById(R.id.re_affirm);
        affirm.setHorizontallyScrolling(true);
        EditText code = findViewById(R.id.re_code);
        code.setHorizontallyScrolling(true);


        Button send_code = findViewById(R.id.re_sendCode);
        back_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Register.this, Login.class));

            }
        });
        send_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input_nickname = nickname.getText().toString();
                String input_email = email.getText().toString();
                String input_password = password.getText().toString();
                String input_affirm = affirm.getText().toString();
                if(email == null) {
                    Toast.makeText(Register.this, "请填写邮箱", Toast.LENGTH_SHORT).show();
                }
                else if (input_password.equals(input_affirm)) {
                    if (input_password.length() < 6) {
                        Toast.makeText(Register.this, "密码长度应不少于六位", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        //请求验证码
                        new Thread(() -> {
                            try {
                                String data = "Email=" + input_email;
                                jsonObject =  Send_data.do_post(Register.this,data,Config.EMAIL_CODE);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (jsonObject == null){
                                Toast.makeText(Register.this, "连接错误", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                runOnUiThread(() -> {
                                    Toast.makeText(Register.this, (String)jsonObject.get("message"), Toast.LENGTH_SHORT).show();
                                });
                            }
                        }).start();
                    }
                } else {
                    Toast.makeText(Register.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                }
            }

        });
        Button re_register = findViewById(R.id.re_register);
        re_register.setOnClickListener(
                new View.OnClickListener() {
                    //内部匿名类
                    @Override
                    public void onClick(View view) {

                        //affirm.setKeyListener(DigitsKeyListener.getInstance(input_limit.no_nextline));
                        String input_nickname = nickname.getText().toString();
                        String input_email = email.getText().toString();
                        String input_password = password.getText().toString();
                        String input_affirm = affirm.getText().toString();
                        String input_code = code.getText().toString();
                        if (input_code.equals("")){
                            Toast.makeText(Register.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                        }
                        else if (input_password.equals(input_affirm)) {
                            if (input_password.length() < 6) {
                                Toast.makeText(Register.this, "密码长度应不少于六位", Toast.LENGTH_SHORT).show();
                            }
                            ///////////////数据传输
                            else {
                                new Thread(() -> {
                                    try {
                                        String data = "Name=" + URLEncoder.encode(input_nickname,"UTF-8")
                                                +"&Passwd=" + URLEncoder.encode(input_password,"UTF-8")
                                                + "&Email=" + URLEncoder.encode(input_email,"UTF-8")
                                                +"&Code=" + URLEncoder.encode(input_code,"UTF-8");
                                        jsonObject = Send_data.do_post(Register.this,data,Config.REGIST_URL);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    if (jsonObject == null){
                                        Toast.makeText(Register.this, "连接错误", Toast.LENGTH_SHORT).show();
                                    }

                                    else if ((int)jsonObject.get("code") == Config.register_success) {
                                        runOnUiThread(() -> {
                                            Toast.makeText(Register.this, "注册成功！", Toast.LENGTH_SHORT).show();
                                            MainActivity.TheUser = new User(input_nickname,input_email,input_password);
                                            Intent login_intent = new Intent();
                                            login_intent.setClass(Register.this, Login.class);
                                            startActivity(login_intent);
                                        });
                                    } else if ((int)jsonObject.get("code") == Config.username_exist) {
                                        runOnUiThread(() -> {
                                            Toast.makeText(Register.this, "邮箱已存在", Toast.LENGTH_SHORT).show();
                                        });
                                    } else {
                                        runOnUiThread(() -> {
                                            Toast.makeText(Register.this, (String)jsonObject.get("message"), Toast.LENGTH_SHORT).show();
                                        });
                                    }

                                }).start();
                            }
                        } else {
                            Toast.makeText(Register.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}